<template>
  <div>

		<el-dialog title="上传证书" center @close = 'closeSendPayNotice' width="400px" :visible.sync="isShow" >
      <el-upload
				class="upload"
				:action="iploadULR"
				:on-remove="handleRemove1"
				:on-change="handleChange1"
				:http-request="uploadSectionFile"
				multiple
				:auto-upload="true"
				:limit="1"
				:file-list="fileList">
				<el-button v-no-more-click:3000 size="small" type="success">&emsp;选择证书&emsp;</el-button>
				<!-- <div slot="tip" class="el-upload__tip">一个文件直接上传,如果有多个文件,请将文件夹压缩后上传</div> -->
			</el-upload>
			<el-button v-no-more-click:3000 size="small" type="primary" @click="submit" class='btn-send'>&emsp;发送证书&emsp;</el-button>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios"
export default {
  data () {
    return {
      isShow: false,
      id: '',
      fileList: [],
      fileListV: [],
      iploadULR: '',
      baseUrl: global.baseurl,
    }
  },
  methods: {
    show (data) {
      this.id = data.id
      this.isShow = true
    },
    submit() {
      let list = document.getElementsByClassName(
        "el-upload-list__item is-ready"
      );
      if (list.length == 0) {
        this.$message({
          type: "warning",
          message: "请选择需要上传的文件！"
        });
      }  else {
        this.submitPost()
      }

    },
    submitPost () {
      // FormData 对象
        let form = new FormData();
        form.append('file', this.fileListV[0].raw)
        form.append('finals', this.id)
				axios({
					method: "post",
					url: this.baseUrl + `/api/finals/${this.id}/upload/cert`,
					data: form,
					headers: {
						"Content-Type": "multipart/form-data",
						Authorization: "Bearer " + this.$store.state.token
					}
					}).then(res => {
					if (res.data.status == "success") {
						this.$message({
							type: "success",
							message: "上传软著证书成功!"
            });
            this.$emit("refresh");
            this.isShow = false;
					} else {
						this.$message({
							type: "error",
							message: res.data.message
						});
						this.fileListV = [];
						this.fileListV = [];
					}
				})
    },
    closeSendPayNotice () {
      this.fileList = [];
      this.fileListV = []
    },
    handleRemove1(file, fileList) {
      this.fileListV = fileList;
    },
    handleChange1(file, fileList) {
      let tempList = [];//可以上传的文件
      let that = this;
      let canUpload = true;//是否可以上传
      let fileName,
          fileNameArr,
          arrLen,
          pos,
          lastName,
          orderNum =''
      for(let i = 0;i<fileList.length;i ++){
        // //验证文件类型
					fileName = fileList[i].name
					pos = fileName.lastIndexOf(".")
        // 后缀名
				lastName = fileName.substring(pos, fileName.length)
        if ( lastName.toLowerCase() !== ".pdf") {
          that.$message.error("文件必须为 pdf");
          canUpload = false;
        }
        if(canUpload){
          tempList.push(fileList[i]);
        }
      }
      this.fileListV.splice(0,this.fileListV.length,...tempList);
      fileList.splice(0, fileList.length,...tempList);
      return
    },
    uploadSectionFile(params) {
    },
  }
}
</script>

<style lang='less' type='text/css'>
  .btn-send {
    margin-top: 20px;
  }
</style>
